package com.kuang.org.controller;

import com.kuang.bascic.utils.Ajax;
import com.kuang.bascic.utils.PageList;
import com.kuang.org.domain.Employee;
import com.kuang.org.query.EmployeeQuery;
import com.kuang.org.service.EmployeeService;
import com.kuang.org.service.EmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/emp")
@Api(value = "部门的API",description="员工相关的CRUD功能")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;
    //restful请求方式的注解 restful参数格式必须都是json格式
    // restful风格就是
    @GetMapping
    @ApiOperation(value = "查询全部员工" ) //：@ApiOperation(value = "通过ID查询" )打在方法上，对方法做描述
    public List<Employee> selecetAllEmployee(){
        List<Employee> employees = employeeService.selectAllEmployee();
        return employees;
    }
    @GetMapping("/{id}")
    public Employee selecetAllEmployeeById(@ApiParam(name="根据id查询员工传的id",required = true)@PathVariable("id") Long id){
        //@ApiParam(name="ID属性",required = true) ：打在参数前，对参数做描述  required:true 表示必须
        //@PathVariable("id") 这个注解的意思就是接收地址里面的值 给后面值
        Employee employee = employeeService.selectEmployeeById(id);
        return employee;
    }
    @PostMapping
    @ApiOperation(value = "分页查询" )
    public List<Employee> selectPage(@RequestBody EmployeeQuery employeeQuery){
        //RequestBody 接受前端穿的json数据格式 ，@ResponseBody表示返回给前端json数据格式
        List<Employee> pageList = employeeService.selectByPage(employeeQuery);
        return pageList;
    }
    @PutMapping
    @ApiOperation(value = "新增和修改" )
    public Ajax updateOrAdd (@RequestBody Employee employee){
        //需要返回ajax的格式 所以建立一个工具类返回的格式 里面有两数据：成功的布尔值，和一条string的消息
        //增加和修改放在一起
        // 使用trycath抱起来抛出异常
        try {
            if (employee.getId()==null){
                System.out.println(employee);
                // 没有id 即为新增
                employeeService.addEmployee(employee);
                return new Ajax();
            }else {
                // 有id的情况为修改
                employeeService.UpdateEmployee(employee);
                return new Ajax();
            }
        }catch (Exception e){
            e.printStackTrace();
            return new Ajax(false,"操作失败");
        }


    }
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除" )
    public Ajax deleteEmployee(@ApiParam(value = "删除id",required = true)@PathVariable() Long id){
        try {
            employeeService.deleteEmployeeById(id);
            return new Ajax();
        }catch (Exception e){
            e.printStackTrace();
            return new Ajax(false,"操作失败");
        }

    }
    @PatchMapping
    public Ajax PatchDelete(@RequestBody List<Long> id ) {
        // 批量删除
        // RequestBody 批量删除也是接受json格式参数
        try {
            employeeService.PatchDelete(id);
            return new Ajax();
        } catch (Exception e) {
            e.printStackTrace();
            return new Ajax(false, "操作失败");
        }
    }

}
